package com.dbxt.student.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.record.Margin;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dbxt.dao.StudentMapper;
import com.dbxt.entity.ClerkRecord;
import com.dbxt.entity.Files;
import com.dbxt.entity.Record;
import com.dbxt.entity.RecordTeacher;
import com.dbxt.entity.Student;

import com.dbxt.student.service.StudentService;
import com.dbxt.util.DateUtil;
import com.dbxt.util.FileUploadUtil;
import com.dbxt.util.RandomUtil;
import com.mysql.cj.xdevapi.JsonArray;

@Controller
@RequestMapping("/student")
public class StudentController {
	@Autowired
	private StudentService studentService;
	
	@Autowired
	private StudentMapper studentMapper;
	//学生上传入职证明图片
	@RequestMapping(value = "uploadJobProve",method = RequestMethod.POST)
	@ResponseBody
	public JSONObject uploadJobProve(MultipartFile picture,HttpSession session,HttpServletRequest request) throws IllegalStateException, IOException, ParseException {
		JSONObject json=new JSONObject();
		int status =1;
		//判断文件是否为空
		if(picture==null) {
			json.put("status", status);
			json.put("message", "上传的入职证明为空");
			return json;
		}
		//判断是否登录
		Student student = (Student) session.getAttribute("student");
		if(student==null) {
			json.put("status", 2);
			json.put("message", "未登录");
			return json;
		}
		//判断文件合法性
		//获取文件后缀
		String fileNameEnds = FileUploadUtil.getFileNameEnds(picture.getOriginalFilename());
		boolean checkImages = FileUploadUtil.CheckImages(fileNameEnds);
			if(checkImages==false) {
				json.put("status", 3);
				json.put("message", "文件不合法，只能上传图片文件");
				return json;
			}
		//获取文件上传路径
		System.setProperty("sun.jnu.encoding", "utf-8");
		String path=request.getServletContext().getRealPath("/files") + "/job/"+student.getSno()+student.getName()+"入职证明"+fileNameEnds;
		String path1=request.getServletContext().getRealPath("/files") + "/job/"+student.getSno()+student.getName()+"入职证明"+".jpg";
		String path2=request.getServletContext().getRealPath("/files") + "/job/"+student.getSno()+student.getName()+"入职证明"+".JPG";
		String path3=request.getServletContext().getRealPath("/files") + "/job/"+student.getSno()+student.getName()+"入职证明"+".png";
		String path4=request.getServletContext().getRealPath("/files") + "/job/"+student.getSno()+student.getName()+"入职证明"+".PNG";
		String path5=request.getServletContext().getRealPath("/files") + "/job/"+student.getSno()+student.getName()+"入职证明"+".jpeg";
		String path6=request.getServletContext().getRealPath("/files") + "/job/"+student.getSno()+student.getName()+"入职证明"+".JPEG";
		//获取文件全名
		String pictureName=student.getSno()+student.getName()+"入职证明"+fileNameEnds;
		
		String pictureName1=student.getSno()+student.getName()+"入职证明"+".png";
		String pictureName2=student.getSno()+student.getName()+"入职证明"+".PNG";
		String pictureName3=student.getSno()+student.getName()+"入职证明"+".jpg";
		String pictureName4=student.getSno()+student.getName()+"入职证明"+".JPG";
		String pictureName5=student.getSno()+student.getName()+"入职证明"+".jpeg";
		String pictureName6=student.getSno()+student.getName()+"入职证明"+".JPEG";
		List<String> list=new ArrayList<>();
		list.add(pictureName1);
		list.add(pictureName2);
		list.add(pictureName3);
		list.add(pictureName4);
		list.add(pictureName5);
		list.add(pictureName6);
		String savePath=request.getServletContext().getRealPath("/files/job/");
			if(!new File(savePath).exists()) {
					//文件夹不存在,创建目录
					new File(savePath).mkdirs();
					//文件存储
					picture.transferTo(new File(path));
					//持久化
					Files files = new Files(pictureName, DateUtil.getCurrentDateTime(), student);
					studentService.upload(files,pictureName);
						json.put("status", 0);
						json.put("message", "操作成功");
						return json;
			}else{//文件存在,直接删除旧文件，直接存储
					new File(path1).delete();
					new File(path2).delete();
					new File(path3).delete();
					new File(path4).delete();
					new File(path5).delete();
					new File(path6).delete();
					//删除关联文件
					studentService.deleteFileByFileName(list);
					//文件存储
					picture.transferTo(new File(path));
					//持久化
					Files files = new Files(pictureName, DateUtil.getCurrentDateTime(), student);
					studentService.upload(files,pictureName);
						json.put("status", 0);
						json.put("message", "操作成功");
						return json;
			}
	}
	
//上传论文
	@RequestMapping(value = "/uploadPaper", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject uploadPaper(MultipartFile paper, HttpServletRequest request)
			throws IllegalStateException, IOException, ParseException {
		JSONObject json = new JSONObject();
		String fileNameEnds = FileUploadUtil.getFileNameEnds(paper.getOriginalFilename());
		int status = 1;
		if (paper.isEmpty()) {
			// 为空返回1
			json.put("status", status);
			json.put("message", "文件为空");
			return json;
		} else if (!FileUploadUtil.CheckFile(fileNameEnds)) {
			json.put("status", 5);
			json.put("message", "文件类型不合法，只能上传Word文档文件");
			return json;
		}
		Student student = (Student) request.getSession().getAttribute("student");
		// 检验文件数量，只能一个任务书一个论文
		/*
		 * if (filesList.size() >= 2) { json.put("status", 3); json.put("message",
		 * "已上传文件数量不合法"); return json; }
		 */
		String path = request.getServletContext().getRealPath("/files") + "/paper/" + student.getSno() +student.getName()+ "论文"
				+ fileNameEnds;
		String path1 = request.getServletContext().getRealPath("/files") + "/paper/" + student.getSno() +student.getName()+ "论文"
				+ ".doc";
		String path2 = request.getServletContext().getRealPath("/files") + "/paper/" + student.getSno() +student.getName()+ "论文"
				+ ".docx";
		String path3 = request.getServletContext().getRealPath("/files") + "/paper/" + student.getSno() +student.getName()+ "论文"
				+ ".DOC";
		String path4 = request.getServletContext().getRealPath("/files") + "/paper/" + student.getSno() +student.getName()+ "论文"
				+ ".DOCX";
		System.setProperty("sun.jnu.encoding", "utf-8");
		// 创建目录
		File savepath = new File(request.getServletContext().getRealPath("/files") + "/paper/");
		// 文件全名
		String filename = student.getSno() +student.getName()+ "论文" + fileNameEnds;
		List<String> list=new ArrayList<>();
		list.add(student.getSno() +student.getName()+ "论文.doc");
		list.add(student.getSno() +student.getName()+ "论文.docx");
		list.add(student.getSno() +student.getName()+ "论文.DOC");
		list.add(student.getSno() +student.getName()+ "论文.DOCX");
		// 如果不存在这个目录，创建
		if (!savepath.exists()) {
			savepath.mkdirs();
			// 删除旧文件后储存
			new File(path1).delete();
			new File(path2).delete();
			new File(path3).delete();
			new File(path4).delete();
			paper.transferTo(new File(path));
			json.put("status", 0);
			json.put("message", "操作成功");
			// 存储完持久化
			Files files = new Files(filename, DateUtil.getCurrentDateTime(), student);
			studentService.upload(files,filename);
			return json;
		} else {
			// 如果有目录，删除旧文件后储存
			new File(path1).delete();
			new File(path2).delete();
			new File(path3).delete();
			new File(path4).delete();
			//删除原来关联的文件
			studentService.deleteFileByFileName(list);
			paper.transferTo(new File(path));
			json.put("status", 0);
			json.put("message", "操作成功");
			// 存储完持久化
			Files files = new Files(filename, DateUtil.getCurrentDateTime(), student);
			studentService.upload(files,filename);
			return json;
		}
	}

//上传任务书
	@ResponseBody
	@RequestMapping(value = "/uploadCharter", method = RequestMethod.POST)
	public JSONObject uploadCharter(MultipartFile charter, HttpServletRequest request)
			throws IllegalStateException, IOException, ParseException {
		JSONObject json = new JSONObject();
		// 文件后缀名
		String fileNameEnds = FileUploadUtil.getFileNameEnds(charter.getOriginalFilename());
		int status = 1;
		if (charter.isEmpty()) {
			// 文件为空
			json.put("status", status);
			json.put("message", "文件为空");
			return json;
		} else if (!FileUploadUtil.CheckFile(fileNameEnds)) {
			// 校验文件是否合法
			json.put("status", 5);
			json.put("message", "文件类型不合法，只能上传Word文档文件");
			return json;
		}
		Student student = (Student) request.getSession().getAttribute("student");
		// 检验文件数量，只能一个任务书一个论文
		/*
		 * if (filesList.size() >= 2) { json.put("status", 3); json.put("message",
		 * "已上传文件数量不合法"); return json; }
		 */
		String path = request.getServletContext().getRealPath("/files") + "/charter/" + student.getSno() +student.getName()+ "任务书"
				+ fileNameEnds;
		String path1 = request.getServletContext().getRealPath("/files") + "/charter/" + student.getSno() +student.getName()+ "任务书"
				+ ".doc";
		String path2 = request.getServletContext().getRealPath("/files") + "/charter/" + student.getSno() +student.getName()+ "任务书"
				+ ".docx";
		String path3 = request.getServletContext().getRealPath("/files") + "/charter/" + student.getSno() +student.getName()+ "任务书"
				+ ".DOC";
		String path4 = request.getServletContext().getRealPath("/files") + "/charter/" + student.getSno() +student.getName()+ "任务书"
				+ ".DOCX";
		System.setProperty("sun.jnu.encoding", "utf-8");
		// 创建目录
		File savepath = new File(request.getServletContext().getRealPath("/files") + "/charter/");
		// 文件全名
		String filename = student.getSno() +student.getName()+ "任务书" + fileNameEnds;
		List<String> list=new ArrayList<>();
		list.add(student.getSno() +student.getName()+ "任务书.doc");
		list.add(student.getSno() +student.getName()+ "任务书.docx");
		list.add(student.getSno() +student.getName()+ "任务书.DOC");
		list.add(student.getSno() +student.getName()+ "任务书.DOCX");
		// 如果不存在这个目录，创建
		if (!savepath.exists()) {
			savepath.mkdirs();
			// 创建完删除旧文件再存
			new File(path1).delete();
			new File(path2).delete();
			new File(path3).delete();
			new File(path4).delete();
			charter.transferTo(new File(path));
			json.put("status", 0);
			json.put("message", "操作成功");
			// 存储完持久化
			Files files = new Files(filename, DateUtil.getCurrentDateTime(), student);
			studentService.upload(files,filename);
			return json;
		} else {
			// 如果有目录删除旧文件再存
			new File(path1).delete();
			new File(path2).delete();
			new File(path3).delete();
			new File(path4).delete();
			//删除原来关联的文件
			studentService.deleteFileByFileName(list);
			charter.transferTo(new File(path));
			json.put("status", 0);
			json.put("message", "操作成功");
			// 存储完持久化
			Files files = new Files(filename, DateUtil.getCurrentDateTime(), student);
			studentService.upload(files,filename);
			return json;
		}
	}

	/*
	 * //删除任务书
	 * 
	 * @RequestMapping(value = "/deleteCharter", method = RequestMethod.GET)
	 * 
	 * @ResponseBody public JSONObject deleteCharter(String charterName,
	 * HttpServletRequest request) { JSONObject json = new JSONObject(); int status
	 * = 1; if ("".equals(charterName) || charterName == null) { // 名字为空返回1
	 * json.put("status", status); json.put("message", "操作失败"); return json; }
	 * String path = request.getServletContext().getRealPath("/files") + "/charter/"
	 * + charterName; // 删除文件 new File(path).delete(); // 删除完持久化
	 * studentService.deleteFile(charterName); json.put("status", 0);
	 * json.put("message", "操作成功"); return json; }
	 * 
	 * //删除论文
	 * 
	 * @RequestMapping(value = "/deletePaper", method = RequestMethod.GET)
	 * 
	 * @ResponseBody public JSONObject deletePaper(String paperName,
	 * HttpServletRequest request) { JSONObject json = new JSONObject(); int status
	 * = 1; if ("".equals(paperName) || paperName == null) { // 名字为空返回1
	 * json.put("status", status); json.put("message", "操作失败"); return json; }
	 * String path = request.getServletContext().getRealPath("/files") + "/paper/" +
	 * paperName; // 删除文件 new File(path).delete(); // 删除完持久化
	 * studentService.deleteFile(paperName); json.put("status", 0);
	 * json.put("message", "操作成功"); return json; }
	 */

//获取学生文件列表
	@RequestMapping(value = "/getFiles", method = RequestMethod.GET)
	@ResponseBody
	public JSONObject getFiles(HttpSession session,Integer sid) {
		JSONObject json = new JSONObject();
		int status = 1;
		//判断学生是否存在
		Student selectByPrimaryKey = studentService.selectByPrimaryKey(sid);
				if(selectByPrimaryKey==null) {
					json.put("status", status);
					json.put("message", "查询的学生不存在");
					return json;
				}
		List<Files> files = studentService.getFiles(sid);
				if (files.size()==0) {
					json.put("status", status);
					json.put("message", "暂未上传文件");
					return json;
				}else {
					json.put("status", 0);
					json.put("message", "操作成功");
					json.put("list", files);
					return json;
				}
	}
//学生申请答辩
	@RequestMapping(value = "/askForDefence", method = RequestMethod.GET)
	@ResponseBody
	public JSONObject askForDefence(HttpSession session, HttpServletRequest request,Integer sid) {
		JSONObject json = new JSONObject();
		int status = 1;
		Student student = (Student) session.getAttribute("student");
		//首先判断学生是否登录
		if(student==null) {
			json.put("status",status);
			json.put("message","学生未登录");
			return json;
		}
		//判断这个学生是不是上传过任务书和论文文件
		List<Files> files = studentService.getFiles(sid);
		if(files.size()<=1) {
			json.put("status",4);
			json.put("message","请上传任务书和论文文件");
			return json;
		}
		//判断这个人是不是已答辩后又进行申请答辩
		Record record = studentService.getRecordBySid(sid);//获取与当前登录学生相关的答辩记录
			if(record==null) {
				//判断是否进行频繁操作
		Object object=request.getServletContext().getAttribute(sid.toString());//获取取消的时间戳
				 if(object!=null) {
					 long cancelTime=(long)object;
					 Date date=new Date();
					 long currentTime = date.getTime();
					 System.out.println("距离上次取消答辩过了:"+(currentTime-cancelTime)+"毫秒");
					 		if(currentTime-cancelTime<300000) { 
								  json.put("status", 4);
								  json.put("message","频繁操作");
								  return json; 
					 		}
				  }
				//到这里没有频繁操作，首先随机分组
				String randomGroup = RandomUtil.group();//随机分组结果，新增答辩记录用
				studentService.askforRecord(sid, randomGroup);//进行新增答辩记录
				Record recordBySid = studentService.getRecordBySid(sid);
				Integer rid = recordBySid.getRid();
				studentService.make(rid);
					json.put("status", 0);
					json.put("message","操作成功");
					json.put("group", randomGroup);
					return json;
				}
			//不==null，接下来是恶意操作判断
			String status2 = record.getStatus();
			if(status2!=null) {
					if("0".equals(status2)) {
						json.put("status", 2);
						json.put("message","您已经申请过答辩了");
						return json;
					}
					if("1".equals(status2)) {
						json.put("status", 3);
						json.put("message","已答辩完成，请勿再次提交答辩申请");
						return json;
					}
			}
			return json;
			}

//学生查看A组答辩情况(专供老师)
	@RequestMapping(value = "/seeDefenceWithGroupA", method = RequestMethod.GET)
	@ResponseBody
	public JSONObject seeDefenceWithGroupA(HttpSession session, HttpServletRequest request,Integer tid) {
		JSONObject json = new JSONObject();
	List<Record> groupA =  studentService.getGroupA(tid);
	if(groupA.size()==0) {
		List<Record> noScoreGroupA = (List<Record>) studentService.selectOnlyGroupA();
		json.put("groupA", noScoreGroupA);
		json.put("status", 0);
		json.put("message", "操作成功");
			return json ;
	}
		json.put("groupA", groupA);
		json.put("status", 0);
		json.put("message", "操作成功");
			return json;
	}

//学生查看B组答辩情况(专供老师)
	@RequestMapping(value = "/seeDefenceWithGroupB", method = RequestMethod.GET)
	@ResponseBody
	public JSONObject seeDefenceWithGroupB(HttpSession session, HttpServletRequest request,Integer tid) {
	JSONObject json = new JSONObject();
	List<Record> groupB = studentService.getGroupB(tid);
	if(groupB.size()==0) {
		List<Record> noScoreGroupB = (List<Record>) studentService.selectOnlyGroupB();
		json.put("groupB", noScoreGroupB);
		json.put("status", 0);
		json.put("message", "操作成功");
			return json ;
	}
		json.put("groupB", groupB);
		json.put("status", 0);
		json.put("message", "操作成功");
			return json;
	}
//学生取消答辩
	@RequestMapping(value = "/cancelDefence", method = RequestMethod.GET)
	@ResponseBody
	public JSONObject cancelDefence(HttpSession session, HttpServletRequest request,Integer sid) {
		JSONObject json = new JSONObject();
		int status = 1;
		// 首先获取当前学生
		Student student = (Student) session.getAttribute("student");
		//检查是否登录
		if(student==null) {
			json.put("status", status);
			json.put("message","未登录");
			return json;
		}
		
		Record record = studentService.getRecordBySid(sid);
		if(record==null) {//未申请答辩就来取消答辩
			json.put("status", 2);
			json.put("message","未申请答辩");
			return json;
		}
		//record不==null，表示已经申请过了，去查他是不是已经开始答辩（标志是有老师已经为他打分了）
		Integer rid = record.getRid();//获取他的答辩rid
		List<RecordTeacher> recordTeacherByRid = studentService.getRecordTeacherByRid(rid);//获取根据sid获取的rid获取到的老师给学生答辩的集合
		for (RecordTeacher recordTeacher : recordTeacherByRid) {
			Integer score = recordTeacher.getScore();
			String comment = recordTeacher.getComment();
			if(score!=null) {
				json.put("status", 3);
				json.put("message","答辩已经开始，不能取消答辩");
				return json;
			}
		}
		//到这里说明没有人给他打分
		//判断是不是有书记员进行记录提交了
		List<ClerkRecord> clerkRecordByRid = studentService.getClerkRecordByRid(rid);
		for (ClerkRecord clerkRecord : clerkRecordByRid) {
			String context = clerkRecord.getContext();
			if(context!=null) {
				json.put("status", 4);
				json.put("message","取消失败，已有记录员提交答辩记录");
				return json;
			}
		}
		studentService.delete(rid);
		studentService.cancelApply(sid);
		//取消完后，记录当前时间，然后加入作用域中
		Date canceDate=new Date();//记录取消的时间
		long time = canceDate.getTime();
		request.getServletContext().setAttribute(sid.toString(), time);
		json.put("status", 0);
		json.put("message","操作成功");
		return json;
	}
	//学生查看答辩列表AB组(书记员专供)
	@RequestMapping(value="/seeDefenceAB",method = RequestMethod.GET)
	@ResponseBody
	public JSONObject seeDefenceAB(HttpSession session,HttpServletRequest request,Integer tid) {
			JSONObject json = new JSONObject();
			int status = 1;
			List<Record> groupA = studentService.seeDefenceABOfA();
			List<Record> groupB = studentService.seeDefenceABOfB();
		if(groupA==null&&groupB==null) {
			json.put("status", status);
			json.put("message", "无人报名");
			return json;
		}
			json.put("groupA", groupA);
			json.put("groupB", groupB);
			json.put("status", 0);
			json.put("message", "操作成功");
				return json;
	}
	/*
	 * //替换已经上传的任务书
	 *//**
		 * 
		 * @param newPaperFile 新上传的论文文件
		 * @param oldFileName  需要替换的文件名字
		 * @return
		 * @throws IOException
		 * @throws IllegalStateException
		 */
	/*
	 * @RequestMapping(value="/changePaperFile",method = RequestMethod.POST)
	 * 
	 * @ResponseBody public JSONObject changePaperFile(MultipartFile
	 * newPaperFile,String oldFileName,HttpSession session,HttpServletRequest
	 * request) throws IllegalStateException, IOException { JSONObject json=new
	 * JSONObject(); int status=1; if(newPaperFile.isEmpty()) { //文件为空返回2
	 * json.put("status", status); json.put("message", "文件为空"); return json; }
	 * //获取文件后缀，验证文件后缀名 boolean checkFile =
	 * FileUploadUtil.CheckFile(newPaperFile.getOriginalFilename()); if(!checkFile)
	 * { json.put("status", 2); json.put("message", "文件类型不合法"); return json; }
	 * //到这一步，文件合法且不为空 Student student =(Student)session.getAttribute("student");
	 * //获取旧文件储存路径 String
	 * path=request.getServletContext().getRealPath("/files")+"/paper/"+oldFileName;
	 * //判断这个文件是不是存在 if(new File(path).exists()) { //存在则删除 new File(path).delete();
	 * //删除完进行新文件上传 String
	 * newFilePath=request.getServletContext().getRealPath("/files")+"/paper/"+
	 * student.getSno()+"paper"+FileUploadUtil.getFileNameEnds(newPaperFile.
	 * getOriginalFilename()); newPaperFile.transferTo(new File(newFilePath));
	 * json.put("status", 0); json.put("message", "操作成功"); return json; }else {
	 * json.put("status", 3); json.put("message", "原文件不存在"); return json; } }
	 * //替换已经上传的任务书
	 *//**
		 * 
		 * @param newPaperFile 新上传的任务书文件
		 * @param oldFileName  需要替换的文件名字
		 * @return
		 * @throws IOException
		 * @throws IllegalStateException
		 *//*
			 * @RequestMapping(value="/changeCharterFile",method = RequestMethod.POST)
			 * 
			 * @ResponseBody public JSONObject changeCharterFile(MultipartFile
			 * newCharterFile,String oldFileName,HttpSession session,HttpServletRequest
			 * request) throws IllegalStateException, IOException { JSONObject json=new
			 * JSONObject(); int status=1; if(newCharterFile.isEmpty()) { //文件为空返回2
			 * json.put("status",status); json.put("message", "传入文件为空"); return json; }
			 * //获取文件后缀，验证文件后缀名 boolean checkFile =
			 * FileUploadUtil.CheckFile(newCharterFile.getOriginalFilename());
			 * if(!checkFile) { json.put("status", 2); json.put("message", "文件类型不合法");
			 * return json; } //到这一步，文件合法且不为空 Student student
			 * =(Student)session.getAttribute("student"); //获取旧文件储存路径 String
			 * path=request.getServletContext().getRealPath("/files")+"/charter/"+
			 * oldFileName; //判断这个文件是不是存在 if(new File(path).exists()) { //存在则删除 new
			 * File(path).delete(); //删除完进行新文件上传 String
			 * newFilePath=request.getServletContext().getRealPath("/files")+"/charter/"+
			 * student.getSno()+"charter"+FileUploadUtil.getFileNameEnds(newCharterFile.
			 * getOriginalFilename()); newCharterFile.transferTo(new File(newFilePath));
			 * json.put("status", 0); json.put("message", "操作成功"); return json; }else {
			 * json.put("status", 3); json.put("message", "原文件不存在"); return json; } }
			 */
//学生查看自己答辩成绩
	@RequestMapping(value="/checkResult",method = RequestMethod.GET)
	@ResponseBody
	public JSONObject checkResult (HttpSession session) {
		JSONObject json=new JSONObject();
		//查看答辩成绩  状态是2已完成才能查看
		Student student=(Student)session.getAttribute("student");
		if(student==null) {
			json.put("status", 3);
			json.put("message", "未登录");
			return json;
		}
		Student result = studentService.checkResult(student.getSid());
		if(result==null) {
			json.put("status", 1);
			json.put("message", "未申请答辩");
			return json;
		}else {
			String recordstatus = result.getRecord().getStatus();
			
			//如果答辩状态是2才能返回json
			if("2".equals(recordstatus)) {
				json.put("status",0);
				json.put("message", "操作成功");
				json.put("result",result);
				return json;
			}else {
				json.put("status",2);
				json.put("message", "答辩未完成无法查看成绩");
				return json;
			}
		}
	}
//学生查看老师对我的评价
@RequestMapping(value="/checkEvaluate",method = RequestMethod.POST)
@ResponseBody
public JSONObject checkEvaluate(HttpSession session) {
	JSONObject json=new JSONObject();
	int status=1;
	//判断是否登录
	Student student = (Student) session.getAttribute("student");
		if(student==null) {
			json.put("status", status);
			json.put("message", "无法查看成绩，未登录");
			return json;
		}
	//判断答辩状态,连答辩记录都没有就是没有申请答辩
	Record recordBySid = studentService.getRecordBySid(student.getSid());
		if(recordBySid==null) {
			json.put("status", 2);
			json.put("message", "无法查看成绩，未申请答辩");
			return json;
			}
		//获取答辩状态
	String status2 = recordBySid.getStatus();
		if("-1".equals(status2)) {//老师暂未同意申请答辩
			json.put("status", 3);
			json.put("message", "无法查看成绩，请等待老师同意答辩申请");
			return json;
		}
		if("0".equals(status2)) {//老师同意但是未进行答辩
			json.put("status", 4);
			json.put("message", "无法查看成绩，未进行答辩");
			return json;
		}
		if("1".equals(status2)) {//答辩中
			json.put("status", 5);
			json.put("message", "无法查看成绩，进行答辩中");
			return json;
		}
		if("2".equals(status2)) {//已经完成答辩了，可以查看，但是要对成绩完整性进行判断
			//获得一个RecordTeacher的列表
			List<RecordTeacher> RecordTeacherList = studentService.checkEvaluate(student.getSid());
				if(RecordTeacherList.size()==0) {//没有人给他打分
					json.put("status", 7);
					json.put("message", "暂时没有老师给你打分");
					return json;
				}
					json.put("status", 0);
					json.put("message", "操作成功");
					json.put("list", RecordTeacherList);
					return json;
		}
		if("3".equals(status2)) {//已经旷考了
			json.put("status", 6);
			json.put("message", "无法查看成绩，已旷考");
			return json;
		}
		json.put("status", status);
		json.put("message", "服务器异常");
		return json;
	}	
//学生上传头像
@RequestMapping(value = "uploadPicture",method = RequestMethod.POST)
@ResponseBody
public JSONObject uploadPicture(MultipartFile picture,HttpSession session,HttpServletRequest request) throws IllegalStateException, IOException {
	JSONObject json=new JSONObject();
	int status =1;
	//判断文件是否为空
	if(picture.isEmpty()) {
		json.put("status", status);
		json.put("message", "上传的头像为空");
		return json;
	}
	//判断是否登录
	Student student = (Student) session.getAttribute("student");
	if(student==null) {
		json.put("status", 2);
		json.put("message", "未登录");
		return json;
	}
	//判断文件合法性
	//获取文件后缀
	String fileNameEnds = FileUploadUtil.getFileNameEnds(picture.getOriginalFilename());
	boolean checkImages = FileUploadUtil.CheckImages(fileNameEnds);
		if(checkImages==false) {
			json.put("status", 3);
			json.put("message", "文件不合法");
			return json;
		}
	//获取文件上传路径
	System.setProperty("sun.jnu.encoding", "utf-8");
	String path=request.getServletContext().getRealPath("/files") + "/picture/"+student.getSno()+"/"+student.getSno()+"picture"+fileNameEnds;
	String pictureName=student.getSno()+"picture"+fileNameEnds;
	String savePath=request.getServletContext().getRealPath("/files") + "/picture/"+student.getSno()+"/";
		if(!new File(savePath).exists()) {
				//文件夹不存在,创建目录
				new File(savePath).mkdirs();
				//文件存储
				picture.transferTo(new File(path));
				//持久化
				studentService.uploadPicture(student.getSid(), pictureName);
					json.put("status", 0);
					json.put("message", "操作成功");
					return json;
		}else{//文件存在,直接删除旧文件，直接存储
				FileUtils.deleteDirectory(new File(savePath));
				//删除完创建
				new File(savePath).mkdirs();
				//文件存储
				picture.transferTo(new File(path));
				//持久化
				studentService.uploadPicture(student.getSid(), pictureName);
					json.put("status", 0);
					json.put("message", "操作成功");
					return json;
		}
}
//学生刷新图片信息
@RequestMapping(value = "flushPicture",method = RequestMethod.GET)
@ResponseBody
public JSONObject flushPicture(Integer sid) {
	JSONObject json=new JSONObject();
	if(sid==null) {
		json.put("status", 1);
		json.put("message", "未传入参数");
		return json;
	}
	String picture = studentService.flushPicture(sid);
	if("".equals(picture)||picture==null) {
		json.put("status", 2);
		json.put("message", "未上传头像");
		return json;
	}
	json.put("status", 0);
	json.put("message", "操作成功");
	json.put("picture", picture);
	return json;
}
//查看学生分组
@RequestMapping(value = "seeGroup",method = RequestMethod.GET)
@ResponseBody
public JSONObject seeGroup(Integer sid) {
	JSONObject json=new JSONObject();
	if(sid==null) {
		json.put("status", 1);
		json.put("message", "未传值");
		return json;
	}
	Record recordBySid = studentService.getRecordBySid(sid);
	if(recordBySid==null) {
		json.put("status", 2);
		json.put("message", "未申请答辩没有分组");
		return json;
	}
	String grouping = recordBySid.getGrouping();
	if(grouping==null||"".equals(grouping)) {
		json.put("status", 3);
		json.put("message", "分组信息为空");
	}
	json.put("status", 0);
	json.put("message", "操作成功");
	json.put("grouping", grouping);
	return json;
}
//上传项目文件
@RequestMapping(value="/uploadProject",method = RequestMethod.POST)
@ResponseBody
public JSONObject uploadProject(MultipartFile project,HttpSession session,HttpServletRequest request) throws IllegalStateException, IOException, ParseException {
	JSONObject json=new JSONObject();
	//判断是否登录
	Student student = (Student) session.getAttribute("student");
		if(student==null) {
			json.put("status", 1);
			json.put("message", "您未登录");
			return json;
		}
		if(project==null) {
			json.put("status", 2);
			json.put("message", "文件为空");
			return json;
		}
	long size = project.getSize();
	System.out.println(size);
		if(size>10485760) {
			json.put("status", 4);
			json.put("message", "文件超过上传大小限制，最多10M,请去除jar包后再次上传");
			return json;
		}
	//获取上传者的学号和id
	String sno = student.getSno();
	//获取文件全名
	String originalFilename = project.getOriginalFilename();
	//定义上传后文件全名
	String filename=sno+"项目"+FileUploadUtil.getFileNameEnds(originalFilename);
	//校验合法性
	boolean uploadProject = FileUploadUtil.uploadProject(FileUploadUtil.getFileNameEnds(originalFilename));
		if(uploadProject==false) {
			json.put("status", 3);
			json.put("message", "文件不合法，只能上传rar或者zip文件");
			return json;
		}
	//定义项目文件完全上传路径
	String path=request.getServletContext().getRealPath("/files")+
			"/project/"+sno+"项目"+FileUploadUtil.getFileNameEnds(originalFilename);
	//定义文件夹位置
	String fileSavePath=request.getServletContext().getRealPath("/files")+"/project/";
		if(!new File(fileSavePath).exists()) {//文件夹不存在
			//创建文件夹
			new File(fileSavePath).mkdirs();
			//文件转存
			project.transferTo(new File(path));
			//持久化
			Files files = new Files(filename, DateUtil.getCurrentDateTime(),student);
			studentService.upload(files, filename);
			
			json.put("status", 0);
			json.put("message", "操作成功");
			return json;
		}else {//文件存在，考虑一个替换文件
			new File(path).delete();
			project.transferTo(new File(path));
			//持久化
			Files files = new Files(filename, DateUtil.getCurrentDateTime(),student);
			studentService.upload(files, filename);
			
			json.put("status", 0);
			json.put("message", "操作成功");
			return json;
		}
}
	}
